Packages

chooseCRANmirror(graphics = FALSE, ind = 1) 
install.packages("rinat")
## Installing package into 'C:/Users/kayla/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'rinat' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\kayla\AppData\Local\Temp\Rtmp276Yh7\downloaded_packages
install.packages("tidyverse")
## Installing package into 'C:/Users/kayla/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'tidyverse' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\kayla\AppData\Local\Temp\Rtmp276Yh7\downloaded_packages
install.packages("sf")
## Installing package into 'C:/Users/kayla/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'sf' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\kayla\AppData\Local\Temp\Rtmp276Yh7\downloaded_packages
install.packages("ggplot2")
## Installing package into 'C:/Users/kayla/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'ggplot2' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\kayla\AppData\Local\Temp\Rtmp276Yh7\downloaded_packages
install.packages("rosm")
## Installing package into 'C:/Users/kayla/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'rosm' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\kayla\AppData\Local\Temp\Rtmp276Yh7\downloaded_packages
install.packages("ggspatial")
## Installing package into 'C:/Users/kayla/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'ggspatial' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\kayla\AppData\Local\Temp\Rtmp276Yh7\downloaded_packages
install.packages("prettymapr")
## Installing package into 'C:/Users/kayla/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'prettymapr' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\kayla\AppData\Local\Temp\Rtmp276Yh7\downloaded_packages
install.packages("terra")
## Installing package into 'C:/Users/kayla/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'terra' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\kayla\AppData\Local\Temp\Rtmp276Yh7\downloaded_packages
install.packages("ggspatial")
## Installing package into 'C:/Users/kayla/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'ggspatial' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\kayla\AppData\Local\Temp\Rtmp276Yh7\downloaded_packages
install.packages("mapview")
## Installing package into 'C:/Users/kayla/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'mapview' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\kayla\AppData\Local\Temp\Rtmp276Yh7\downloaded_packages
install.packages("leafpop")
## Installing package into 'C:/Users/kayla/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'leafpop' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\kayla\AppData\Local\Temp\Rtmp276Yh7\downloaded_packages
install.packages("viridis")
## Installing package into 'C:/Users/kayla/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'viridis' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\kayla\AppData\Local\Temp\Rtmp276Yh7\downloaded_packages
library(terra)
## Warning: package 'terra' was built under R version 4.4.3
## terra 1.8.29
library(ggmap)
## Warning: package 'ggmap' was built under R version 4.4.2
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.4.2
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
##   Stadia Maps' Terms of Service: <https://stadiamaps.com/terms-of-service/>
##   OpenStreetMap's Tile Usage Policy: <https://operations.osmfoundation.org/policies/tiles/>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
## 
## Attaching package: 'ggmap'
## The following object is masked from 'package:terra':
## 
##     inset
library(geodata)
## Warning: package 'geodata' was built under R version 4.4.2
library(tmap)
## Warning: package 'tmap' was built under R version 4.4.2
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 4.4.2
library(cowplot)
## Warning: package 'cowplot' was built under R version 4.4.2
## 
## Attaching package: 'cowplot'
## The following object is masked from 'package:ggmap':
## 
##     theme_nothing
library(ggpubr)
## Warning: package 'ggpubr' was built under R version 4.4.2
## 
## Attaching package: 'ggpubr'
## The following object is masked from 'package:cowplot':
## 
##     get_legend
## The following object is masked from 'package:terra':
## 
##     rotate

Calling iNaturalist locality data

#Downloaded the csv files for Diplodus cervinus as the Northern Hemisphere was isolated under a different taxon_name - makes sense as they are two different species but they were subspecies - I seareched the common name
#I then binded these to get a data frame of 1148 observations
zebra_SH <- read.csv("observations-zebra-SH.csv")
zebra_NH <- read.csv("observations-zebra-NH.csv")
zebra <- rbind(zebra_SH, zebra_NH)

#Call the Diplodus sargus/ Diplodus capensis data directly from iNat because it is too large to download
library(rinat)
## Warning: package 'rinat' was built under R version 4.4.2
white <- get_inat_obs(taxon_name = "Diplodus capensis",
                      maxresults = 1000)

#View the first few rows of data
head(zebra)
##         id                                 uuid       observed_on_string
## 1   579530 63f90c83-d841-4269-9c78-ba72bbe849b5                 13-03-11
## 2   647725 ac23bc0e-084f-4c0c-840d-313266c40e49                 02-01-09
## 3   890340 1b5ddc8f-a827-4adc-9cd6-68cd9328d6b5                 16-09-14
## 4  1838887 0fd61396-b36f-4904-a176-59e873e0e063                 02-07-15
## 5  8339338 93d1f704-5249-4484-ba2b-45a1b278830a 2017/07/25 10:32 AM CEST
## 6 10448096 f6ccd0f6-d2f5-4f56-b08f-8440560d12dc  2018/03/24 12:45 PM CAT
##   observed_on        time_observed_at                  time_zone user_id
## 1    13-03-11                         Eastern Time (US & Canada)   19061
## 2    02-01-09                         Eastern Time (US & Canada)   19061
## 3    16-09-14                         Eastern Time (US & Canada)   19061
## 4    02-07-15                         Eastern Time (US & Canada)   19061
## 5    25-07-17 2017-07-25 08:32:00 UTC                      Paris  173306
## 6    24-03-18 2018-03-24 11:45:00 UTC        West Central Africa  738093
##   user_login     user_name              created_at              updated_at
## 1     seasav               2014-03-23 17:38:58 UTC 2018-06-15 02:14:28 UTC
## 2     seasav               2014-04-29 01:02:08 UTC 2018-01-02 06:20:51 UTC
## 3     seasav               2014-09-22 23:02:08 UTC 2018-04-16 12:08:00 UTC
## 4     seasav               2015-08-06 22:30:21 UTC 2017-11-08 00:56:55 UTC
## 5    zanskar David Renoult 2017-10-10 11:08:45 UTC 2018-05-25 23:53:23 UTC
## 6   caperadd     Cape RADD 2018-03-27 12:49:20 UTC 2018-05-25 23:53:07 UTC
##   quality_grade  license                                               url
## 1      research CC-BY-NC    http://www.inaturalist.org/observations/579530
## 2      research CC-BY-NC    http://www.inaturalist.org/observations/647725
## 3      research CC-BY-NC    http://www.inaturalist.org/observations/890340
## 4      research CC-BY-NC   http://www.inaturalist.org/observations/1838887
## 5      research CC-BY-NC  https://www.inaturalist.org/observations/8339338
## 6      research CC-BY-NC https://www.inaturalist.org/observations/10448096
##                                                                    image_url
## 1    https://inaturalist-open-data.s3.amazonaws.com/photos/737163/medium.jpg
## 2    https://inaturalist-open-data.s3.amazonaws.com/photos/816865/medium.jpg
## 3   https://inaturalist-open-data.s3.amazonaws.com/photos/1131256/medium.JPG
## 4   https://inaturalist-open-data.s3.amazonaws.com/photos/2233877/medium.jpg
## 5 https://inaturalist-open-data.s3.amazonaws.com/photos/11082367/medium.jpeg
## 6  https://inaturalist-open-data.s3.amazonaws.com/photos/14522625/medium.jpg
##   sound_url tag_list
## 1        NA         
## 2        NA         
## 3        NA         
## 4        NA         
## 5        NA         
## 6        NA         
##                                                                                                         description
## 1                                                                                                             Zebra
## 2 Aka Zebra Seabream. D. Hottentotus is considered an endemic South African species by Heemstra & Heemstra (2004). 
## 3                                                                                                                  
## 4                                                                                                                  
## 5                                                                                                                  
## 6                                                                                  C:\\DCIM\\106GOPRO\\GOPR0697.GPR
##   num_identification_agreements num_identification_disagreements
## 1                             1                                0
## 2                             2                                1
## 3                             1                                0
## 4                             1                                0
## 5                             2                                0
## 6                             3                                0
##   captive_cultivated oauth_application_id
## 1              FALSE                   NA
## 2              FALSE                   NA
## 3              FALSE                   NA
## 4              FALSE                    3
## 5              FALSE                   NA
## 6              FALSE                   NA
##                                        place_guess  latitude longitude
## 1             Fontao Wreck, Umhlanga, South Africa -29.75067  31.10109
## 2 Aliwal Shoal Marine Protected Area, South Africa -30.24763  30.82628
## 3              T Barge Wreck, Durban, South Africa -29.78345  31.09509
## 4 Aliwal Shoal Marine Protected Area, South Africa -30.24763  30.82628
## 5          St. Lucia, Durban, 3936, Afrique du Sud -28.12619  32.56124
## 6                                   Windmill Beach -34.20099  18.45727
##   positional_accuracy private_place_guess private_latitude private_longitude
## 1                1794                  NA               NA                NA
## 2                2732                  NA               NA                NA
## 3                3388                  NA               NA                NA
## 4                2732                  NA               NA                NA
## 5                  31                  NA               NA                NA
## 6                  83                  NA               NA                NA
##   public_positional_accuracy geoprivacy taxon_geoprivacy coordinates_obscured
## 1                       1794                        open                FALSE
## 2                       2732                        open                FALSE
## 3                       3388                        open                FALSE
## 4                       2732                        open                FALSE
## 5                         31                        open                FALSE
## 6                         83                        open                FALSE
##   positioning_method positioning_device        species_guess
## 1                                manual           Zebra Fish
## 2                                manual Diplodus hottentotus
## 3                                manual Diplodus hottentotus
## 4                                manual Diplodus hottentotus
## 5                                                 Zebra Fish
## 6                                                 Zebra Fish
##        scientific_name common_name iconic_taxon_name taxon_id
## 1 Diplodus hottentotus       Zebra    Actinopterygii   445869
## 2 Diplodus hottentotus       Zebra    Actinopterygii   445869
## 3 Diplodus hottentotus       Zebra    Actinopterygii   445869
## 4 Diplodus hottentotus       Zebra    Actinopterygii   445869
## 5 Diplodus hottentotus       Zebra    Actinopterygii   445869
## 6 Diplodus hottentotus       Zebra    Actinopterygii   445869
##                  ocean  X X.1            X.2      X.3   X.4   X.5   X.6
## 1         Indian Ocean NA  NA                                          
## 2         Indian Ocean NA  NA                                          
## 3         Indian Ocean NA  NA                                          
## 4         Indian Ocean NA  NA                                          
## 5         Indian Ocean NA  NA          Ocean    min_Y min_X max_Y max_X
## 6 South Atlantic Ocean NA  NA Southern Ocean -85.5625  -180   -60   180
head(white)
##     scientific_name                  datetime           description
## 1 Diplodus capensis 2025-02-23 12:49:00 +0200 Photo Melissa Petford
## 2 Diplodus capensis 2025-02-20 12:15:56 +0200                      
## 3 Diplodus capensis 2025-02-20 12:13:26 +0200                      
## 4 Diplodus capensis 2025-02-16 00:00:00 +0200                      
## 5 Diplodus capensis 2025-02-03 13:07:00 +0200                      
## 6 Diplodus capensis 2025-02-06 16:53:11 +0200                      
##                                               place_guess  latitude longitude
## 1                            City of Cape Town, ZA-WC, ZA -34.23098  18.47682
## 2                                               Sudafrica -29.53428  31.22452
## 3                                               Sudafrica -29.54750  31.21291
## 4               Brighton Beach, Bluff, 4052, South Africa -29.93333  31.01278
## 5 Jesser Point, Sodwana Bay, St Lucia, 3974, South Africa -27.53895  32.67974
## 6                                       Cacadu, ZA-EC, ZA -33.68985  26.67569
##   tag_list         common_name
## 1          Cape White Seabream
## 2          Cape White Seabream
## 3          Cape White Seabream
## 4          Cape White Seabream
## 5          Cape White Seabream
## 6          Cape White Seabream
##                                                  url
## 1 https://www.inaturalist.org/observations/262833075
## 2 https://www.inaturalist.org/observations/262412024
## 3 https://www.inaturalist.org/observations/262412008
## 4 https://www.inaturalist.org/observations/261940375
## 5 https://www.inaturalist.org/observations/261108812
## 6 https://www.inaturalist.org/observations/261014306
##                                                                     image_url
## 1 https://inaturalist-open-data.s3.amazonaws.com/photos/472057170/medium.jpeg
## 2 https://inaturalist-open-data.s3.amazonaws.com/photos/471421109/medium.jpeg
## 3 https://inaturalist-open-data.s3.amazonaws.com/photos/471421036/medium.jpeg
## 4  https://inaturalist-open-data.s3.amazonaws.com/photos/470678422/medium.jpg
## 5  https://inaturalist-open-data.s3.amazonaws.com/photos/469033548/medium.jpg
## 6  https://inaturalist-open-data.s3.amazonaws.com/photos/468845437/medium.jpg
##         user_login        id       species_guess iconic_taxon_name taxon_id
## 1 ryanvanhuyssteen 262833075   Diplodus capensis    Actinopterygii   118668
## 2      umbertuccio 262412024   Diplodus capensis    Actinopterygii   118668
## 3      umbertuccio 262412008 Cape White Seabream    Actinopterygii   118668
## 4      erwinsieben 261940375 Cape White Seabream    Actinopterygii   118668
## 5       peterswart 261108812              Dassie    Actinopterygii   118668
## 6     henrydelange 261014306 Cape White Seabream    Actinopterygii   118668
##   num_identification_agreements num_identification_disagreements
## 1                             0                                0
## 2                             0                                0
## 3                             0                                0
## 4                             0                                0
## 5                             1                                0
## 6                             0                                0
##    observed_on_string observed_on        time_observed_at time_zone
## 1 2025/02/23 12:49 PM  2025-02-23 2025-02-23 10:49:00 UTC  Pretoria
## 2 2025-02-20 12:15:56  2025-02-20 2025-02-20 10:15:56 UTC  Pretoria
## 3 2025-02-20 12:13:26  2025-02-20 2025-02-20 10:13:26 UTC  Pretoria
## 4          2025-02-16  2025-02-16                          Pretoria
## 5  2025/02/03 1:07 PM  2025-02-03 2025-02-03 11:07:00 UTC  Pretoria
## 6 2025-02-06 16:53:11  2025-02-06 2025-02-06 14:53:11 UTC  Pretoria
##   positional_accuracy public_positional_accuracy geoprivacy taxon_geoprivacy
## 1                 250                        250                        open
## 2                4496                       4496                        open
## 3                 826                        826                        open
## 4                 146                        146                        open
## 5                  50                         50                        open
## 6                  10                         10                        open
##   coordinates_obscured positioning_method positioning_device user_id
## 1                false                                         97441
## 2                false                gps                gps 2696038
## 3                false                gps                gps 2696038
## 4                false                                       2852980
## 5                false                                       2534793
## 6                false                                        552380
##            user_name              created_at              updated_at
## 1 Ryan van Huyssteen 2025-02-23 17:20:50 UTC 2025-02-23 17:20:50 UTC
## 2                    2025-02-20 18:43:27 UTC 2025-02-23 14:55:23 UTC
## 3                    2025-02-20 18:43:10 UTC 2025-02-20 18:43:23 UTC
## 4       Erwin Sieben 2025-02-17 03:45:16 UTC 2025-02-17 04:45:06 UTC
## 5        Peter Swart 2025-02-09 11:23:02 UTC 2025-02-22 13:52:51 UTC
## 6     Henry de Lange 2025-02-08 17:05:06 UTC 2025-02-08 20:39:37 UTC
##   quality_grade  license sound_url oauth_application_id captive_cultivated
## 1      needs_id CC-BY-NC        NA                   NA              false
## 2      research CC-BY-NC        NA                    2              false
## 3      needs_id CC-BY-NC        NA                    2              false
## 4      research      CC0        NA                   NA              false
## 5      needs_id CC-BY-NC        NA                   NA              false
## 6      research    CC-BY        NA                  179              false

Now that I have read my data into R I want to filter it

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.4.2
## Warning: package 'tidyr' was built under R version 4.4.2
## Warning: package 'purrr' was built under R version 4.4.2
## Warning: package 'dplyr' was built under R version 4.4.2
## Warning: package 'stringr' was built under R version 4.4.2
## Warning: package 'forcats' was built under R version 4.4.2
## Warning: package 'lubridate' was built under R version 4.4.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.4     ✔ tibble    3.2.1
## ✔ purrr     1.0.4     ✔ tidyr     1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::combine()   masks gridExtra::combine()
## ✖ tidyr::extract()   masks terra::extract()
## ✖ dplyr::filter()    masks stats::filter()
## ✖ dplyr::lag()       masks stats::lag()
## ✖ lubridate::stamp() masks cowplot::stamp()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
#Filter returned observations by a range of column attribute criteria
zebra <- zebra %>% filter(!is.na(latitude) & 
                              captive_cultivated == "FALSE" &
                              quality_grade == "research")

white <- white %>% filter(!is.na(latitude) & captive_cultivated == "false" &
                quality_grade == "research")


class(zebra)
## [1] "data.frame"
class(white)
## [1] "data.frame"
#Subsetting based on attributes - I only want certain columns
columns <- c("id", "scientific_name", "description", "latitude", "longitude", "common_name", "url", "species_guess", "positional_accuracy", "user_name")

zebrasub <- zebra[,columns]

whitesub <- white[,columns]


#binding my dataframes
diplodus <- rbind(zebrasub, whitesub)
class(diplodus)
## [1] "data.frame"

Both my datasets are data.frames but I need them to be spatial objects

library(sf)
## Warning: package 'sf' was built under R version 4.4.2
## Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.1; sf_use_s2() is TRUE
#Make the dataframe a spatial object of class = "sf"
diplodus <- st_as_sf(diplodus,coords = c("longitude", "latitude"), crs = 4326)
class(diplodus)
## [1] "sf"         "data.frame"
names(diplodus)
## [1] "id"                  "scientific_name"     "description"        
## [4] "common_name"         "url"                 "species_guess"      
## [7] "positional_accuracy" "user_name"           "geometry"

Adding a basemap

library(rosm)
## Warning: package 'rosm' was built under R version 4.4.2
library(ggspatial)
## Warning: package 'ggspatial' was built under R version 4.4.2
ggplot() + 
  annotation_map_tile(type = "cartolight", progress = "none") + 
  geom_sf(data=diplodus, aes(color = (scientific_name)))

Interactive Map

library(leaflet)
## Warning: package 'leaflet' was built under R version 4.4.2
library(htmltools)
## Warning: package 'htmltools' was built under R version 4.4.2
color_palette <- colorFactor(palette = c( "yellow", "blue", "purple"), domain =diplodus$scientific_name)
leaflet() %>%
  # Add default OpenStreetMap map tiles
  addTiles(group = "Default") %>%  
  # Add our points
  addCircleMarkers(data = diplodus,
                   group = "Diplodus",
                   radius = 0.25, 
                   color = ~color_palette(scientific_name))  %>%
  addLegend("bottomright", pal = color_palette, values = diplodus$scientific_name,
            title = "Scientific Name", opacity = 0.5)

Common sense checks - with live url links

library(mapview)
## Warning: package 'mapview' was built under R version 4.4.2
library(leafpop)
## Warning: package 'leafpop' was built under R version 4.4.2
library(viridis)
## Warning: package 'viridis' was built under R version 4.4.2
## Loading required package: viridisLite
## Warning: package 'viridisLite' was built under R version 4.4.2
#Define a colour palette
fish_palette <- colorRampPalette(c("yellow", "purple", "blue"))
ldiplodus <- diplodus %>%
  mutate(url = paste("<b><a href='", url, "'>Link to iNat observation</a></b>"))

mapview(diplodus, 
        zcol = "scientific_name",
        col.region = fish_palette(3),
        popup = 
          popupTable(ldiplodus,
            zcol = c("user_name", "url", "scientific_name")), cex = 3, alpha = 0.1, lwd = 0.5, legend = TRUE) 

South Africa Focus + Addin MPAs

zebra <- zebra %>% filter(!is.na(latitude) & 
                            latitude < 0 &
                              captive_cultivated == "FALSE" &
                              quality_grade == "research")

white <- white %>% filter(!is.na(latitude) &
                          latitude < 0 &
                             captive_cultivated == "false" &
                quality_grade == "research")
zebraSA <- zebra[,columns]

whiteSA <- white[,columns]

diplodusSA <- rbind(whiteSA, zebraSA)

MPA <- st_read("SAMPAZ_OR_2024_Q3.shp")
## Reading layer `SAMPAZ_OR_2024_Q3' from data source 
##   `C:\Users\kayla\OneDrive\Documents\GIT\Diplodus-GIS\SAMPAZ_OR_2024_Q3.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 161 features and 11 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 14.16181 ymin: -50.23333 xmax: 43.0397 ymax: -26.85786
## Geodetic CRS:  GCS_LatLong_WGS84
st_crs(MPA)
## Coordinate Reference System:
##   User input: GCS_LatLong_WGS84 
##   wkt:
## GEOGCRS["GCS_LatLong_WGS84",
##     DATUM["World Geodetic System 1984",
##         ELLIPSOID["WGS 84",6378137,298.257223563,
##             LENGTHUNIT["metre",1]],
##         ID["EPSG",6326]],
##     PRIMEM["0.0",0,
##         ANGLEUNIT["Degrees",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["longitude",east,
##             ORDER[1],
##             ANGLEUNIT["Degrees",0.0174532925199433]],
##         AXIS["latitude",north,
##             ORDER[2],
##             ANGLEUNIT["Degrees",0.0174532925199433]]]
# Ensure MPA is a spatial object
MPA <- st_as_sf(MPA)

# Define the bounding box for South Africa's coastline (adjust if needed)
south_africa_bbox <- st_bbox(c(xmin = 16, xmax = 33, ymin = -35, ymax = -22), crs = st_crs(MPA))

# Filter MPAs that intersect with the bounding box
coastal_MPAs <- MPA %>%
  filter(st_intersects(MPA, st_as_sfc(south_africa_bbox), sparse = FALSE))
## Warning: Using one column matrices in `filter()` was deprecated in dplyr 1.1.0.
## ℹ Please use one dimensional logical vectors instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
diplodusSA$scientific_name <- as.factor(diplodusSA$scientific_name)

color_paletteSA <- colorFactor(palette = c( "yellow", "lightblue"), domain =diplodus$scientific_name)

diplodusSA <- st_as_sf(diplodusSA,coords = c("longitude", "latitude"), crs = 4326)
ldiplodusSA <- diplodusSA %>%
  mutate(url = paste("<b><a href='", url, "'>Link to iNat observation</a></b>"))

mapview(diplodusSA,
        zcol = "scientific_name",
        col.region = viridis(2, option = "C") , 
        popup = 
          popupTable(ldiplodusSA,
            zcol = c("user_name", "url", "scientific_name")), cex = 1.5, alpha = 0.6, lwd = 0.3, legend = TRUE) +mapview(coastal_MPAs,col.regions = "deepskyblue", alpha = 0.3, lwd = 1)

======= >>>>>>> b3e5364d4184ee420f5ba3085a5361192590ac03